package com.ry.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ry.aspect.annotation.MenuResult;
import com.ry.aspect.annotation.ModuleResult;
import com.ry.aspect.annotation.ParamDeal;
import com.ry.aspect.annotation.RedisLock;
import com.ry.bean.People;
import com.ry.service.IPeopleService;
import com.ry.util.DateUtil8;
import com.ry.util.MessageUtil;

@RestController
@RequestMapping("/index")
public class IndexController {

	@Autowired
    private MessageUtil messageUtil;
	
	@Autowired
	private IPeopleService peopleService;
	
	@RequestMapping("/hello")
	public String hello(HttpServletRequest request) {
		String language = request.getHeader("Accept-Language");
		System.out.println(language);
		String title = messageUtil.get("user.title");
		System.out.println(title);
//		return "hello";
		return title;
	}
	
	@RequestMapping("/base")
	@ParamDeal(useDict=true)
	public People hello(People people, HttpServletRequest request) {
		String language = request.getHeader("Accept-Language");
		System.out.println("请求头语言 ： "+language);
		String title = messageUtil.get("user.title");
		System.out.println("user.title" + title);
		String lang = people.getDictLanguage();
		System.out.println("实体类语言： "+lang);
		
		System.out.println("接到的时间 "+people.getCreatedOn());
		Date date = new Date();
		people.setCreatedOn(date);
		
//		return people.getCreatedOn().toString();
		return people;
	}
	
	@RequestMapping("/menu")
	@MenuResult
	public List menu(People people, HttpServletRequest request) {
		String language = request.getHeader("Accept-Language");
		System.out.println(language);
		
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map = new HashMap<String, String>();
		map.put("menuName", "menu.system");
		list.add(map);
		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("menuName", "menu.itmeAlert");
		list.add(map1);
		Map<String, String> map2 = new HashMap<String, String>();
		map2.put("menuName", "menu.jobList");
		list.add(map2); 
		
//		for (Map<String, String> m : list) {
//			String menuName = m.get("menuName");
//			String menuNamei18 = messageUtil.get(menuName);
//			m.put("menuName", menuNamei18);
//		}
		
		return list;
	}
	
	@RequestMapping("/list")
	public List list(People people) {
		List<People> list = peopleService.list(people);
		return list;
	}

	@RequestMapping("/listAll")
	public List listAll() {
		List<People> list = peopleService.listAll();
		return list;
	}
	
	@RequestMapping("/add")
	public String add(People people) {
		int a = peopleService.insert(people);
		return Integer.toString(a);
	}
	
	
	@RequestMapping("/listPeople")
	@ModuleResult
	public List testMenu() {
		String lm = "{'zh-CN':'系统管理','en-US':'system manager','zh-TW':'系統管理'}";
		List<People> list = new ArrayList<People>();
		//速度测试 10W ok
		for(int i=0;i<10;i++) {
			People people = new People();
			people.setLangMapping(lm);
			list.add(people);	
		}
		System.out.println("over");
		
		return list;
	}
	
	@RedisLock
	@RequestMapping("/testLock")
	public synchronized String testLock() {
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String time = DateUtil8.getNowTime_EN();
		System.out.println(time);
		
		return time;
	}
	
	
}
